package com.zjitc.service.imp;

import com.google.gson.Gson;
import com.zjitc.dao.CarDao;
import com.zjitc.dao.ProductDao;
import com.zjitc.model.Car;
import com.zjitc.model.CarItem;
import com.zjitc.model.Product;
import com.zjitc.model.User;
import com.zjitc.service.CarService;
import com.zjitc.utils.redis.MyRedis;

import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * Create by IntelliJ IDEA
 *
 * @author: lhl
 * @date-Time: 2018/4/5 0005
 * @description:
 */
@Service
public class CarServiceImpl implements CarService {
    private static Logger logger = Logger.getLogger(ProductServiceImpl.class);
    @Autowired
    private CarDao dao;
    @Autowired
    private ProductDao productDao;
    @Autowired
    private MyRedis redis;

    @Override
    public void add(String uid, String pid, int count) {
        Gson gson = new Gson();
        Car car = dao.findCar(uid, pid);
        if (car == null) {
            dao.add(uid, pid, count);
            Car car2 = dao.findCar(uid, pid);
            redis.addCathc(uid + "car", pid, gson.toJson(car2));
        } else {
            count += car.getCount();
            dao.update(uid, pid, count);
            car.setCount(count);
            redis.alertById(uid + "car", pid, gson.toJson(car));
        }
    }

    @Override
    public List<CarItem> findAll(String uid) {
        List<String> list = redis.hvals(uid + "car");
        if (list != null && !"".equals(list)) {
            if (list.size() != 0) {
                Gson gson = new Gson();
                List<Car> carList = new ArrayList<>();
                for (String str : list) {
                    Car car = gson.fromJson(str, Car.class);
                    carList.add(car);
                }
                List<CarItem> items = new ArrayList<>();
                for (Car car : carList) {
                    String s = redis.getOneById("products", car.getPid());
                    if (s != null) {
                        Product p = gson.fromJson(s, Product.class);
                        items.add(new CarItem(car.getCount(), p, car.getCount() * p.getShopPrice()));
                    } else {
                        Product p = productDao.findByPid(car.getPid());
                        items.add(new CarItem(car.getCount(), p, car.getCount() * p.getShopPrice()));
                    }
                }
                return items;
            }
        }
        logger.debug("redis服务器出现问题，请维修人员尽快处理");
        List<Car> shops = dao.findAllCars(uid);
        List<CarItem> items = new ArrayList<>();
        for (Car shop : shops) {
            Product p = productDao.findByPid(shop.getPid());
            items.add(new CarItem(shop.getCount(), p, shop.getCount() * p.getShopPrice()));
        }
        return items;
    }

    @Override
    public void clear(String uid) {
        dao.clear(uid);
        redis.del(uid + "car");
    }

    @Override
    public void delete(String pid, HttpSession session) {
        User user = (User) session.getAttribute("user");
        dao.delete(pid);
        redis.deleteById(user.getUid() + "car", pid);
    }
}
